package com.ruoyi.finance.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.domain.DefaultAccountSet;
import com.ruoyi.finance.mapper.DefaultAccountSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.AccountSetMapper;
import com.ruoyi.finance.domain.AccountSet;
import com.ruoyi.finance.service.IAccountSetService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套管理Service业务层处理
 * 
 * @author 李伟
 * @date 2025-02-24
 */
@Service
public class AccountSetServiceImpl implements IAccountSetService 
{
    @Autowired
    private AccountSetMapper accountSetMapper;
    @Autowired
    DefaultAccountSetMapper defaultAccountSetMapper;
    @Autowired
    private DefaultAccountSetServiceImpl defaultAccountSetService;

    /**
     * 查询账套管理
     * 
     * @param accountId 账套管理主键
     * @return 账套管理
     */
    @Override
    public AccountSet selectAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.selectAccountSetByAccountId(accountId);
    }

    /**
     * 查询账套管理列表
     * 
     * @param accountSet 账套管理
     * @return 账套管理
     */
    @Override
    public List<AccountSet> selectAccountSetList(AccountSet accountSet)
    {
        return accountSetMapper.selectAccountSetList(accountSet);
    }

    /**
     * 新增账套管理
     * 
     * @param accountSet 账套管理
     * @return 结果
     */
    @Override
    public int insertAccountSet(AccountSet accountSet) {
        // 非空验证
        if (accountSet == null) {
            throw new IllegalArgumentException("账套管理对象不能为空");
        }

        // 字段验证
        if (StringUtils.isEmpty(accountSet.getAccountName())) {
            throw new IllegalArgumentException("账套名称不能为空");
        }
        if (StringUtils.isEmpty(accountSet.getAccountName())) {
            throw new IllegalArgumentException("单位名称不能为空");
        }
        if (StringUtils.isEmpty(accountSet.getContactPhone())) {
            throw new IllegalArgumentException("手机号不能为空");
        }
        if (!isValidPhoneNumber(accountSet.getContactPhone())) {
            throw new IllegalArgumentException("手机号格式不正确");
        }

        // 唯一性验证
        AccountSet existingAccountSet = accountSetMapper.selectAccountSetByAccountId(accountSet.getAccountId());
        if (existingAccountSet != null) {
            throw new IllegalArgumentException("已存在相同ID的账套");
        }

        // 设置创建时间
        accountSet.setCreateTime(DateUtils.getNowDate());

        // 插入数据
        return accountSetMapper.insertAccountSet(accountSet);
    }

    /**
     * 验证手机号是否有效
     *
     * @param phoneNumber 手机号
     * @return 是否有效
     */
    private boolean isValidPhoneNumber(String phoneNumber) {
        // 正则表达式验证手机号
        String regex = "^1[3-9]\\d{9}$";
        return phoneNumber.matches(regex);
    }


    /**
     * 修改账套管理
     * 
     * @param accountSet 账套管理
     * @return 结果
     */
    @Override
    public int updateAccountSet(AccountSet accountSet)
    {
        accountSet.setUpdateTime(DateUtils.getNowDate());
        return accountSetMapper.updateAccountSet(accountSet);
    }

    /**
     * 批量删除账套管理
     * 
     * @param accountIds 需要删除的账套管理主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountIds(Long[] accountIds)
    {
        return accountSetMapper.deleteAccountSetByAccountIds(accountIds);
    }

    /**
     * 删除账套管理信息
     * 
     * @param accountId 账套管理主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.deleteAccountSetByAccountId(accountId);
    }

    @Override
    public List<AccountSet> selectAccountSetListAllbyUserid() {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        return accountSetMapper.selectAccountSetListAllbyUserid(loginUser.getUserId());
    }

    @Override
    @Transactional
    public int checkDefaultAccountSet(Long accountId) {
        Long userId = SecurityUtils.getUserId();
        //先查出用户默认的账套表
        DefaultAccountSet defaultAccountSet=defaultAccountSetMapper.selectDefaultAccountSetByuserid(userId);
        Long accountSetId = defaultAccountSet.getAccountSetId();
        Long id = defaultAccountSet.getId();
        if (accountSetId == accountId) {
            throw new RuntimeException("已是默认账套,请勿重复操作");
        }else {
            defaultAccountSetMapper.deleteDefaultAccountSetById(id);
            DefaultAccountSet defaultAccountSet1 = new DefaultAccountSet();
            defaultAccountSet1.setAccountSetId(accountId);
            defaultAccountSet1.setUserId(userId);
            int insert = defaultAccountSetMapper.insertDefaultAccountSet(defaultAccountSet1);
            return insert;
        }

    }
}
